package com.zhanbo.zhanbowymusic.play;

import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.os.Bundle;

import androidx.annotation.NonNull;

import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.widget.MediaController;
import android.widget.SeekBar;

import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.MVListBean;
import com.mwx.baselib.net.AppConfig;
import com.mwx.baselib.utils.MLog;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentMvPlayBinding;
import com.zhanbo.zhanbowymusic.music.MusicController;

/**
 *MV播放页面
 */
public class MvPlayFragment extends BaseFragment<FragmentMvPlayBinding> implements View.OnClickListener {

    private MVListBean.ItemsDTO playMVBean;

    public static MvPlayFragment newInstance(MVListBean.ItemsDTO playMVBean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("data",playMVBean);
        MvPlayFragment fragment = new MvPlayFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.fragment_mv_play;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initData() {
        MusicController.getInstance().pause();
    playMVBean = (MVListBean.ItemsDTO) getArguments().getSerializable("data");
        //加载指定的视频文件
        binding.ttvMv.setVideoPath(AppConfig.URL+(TextUtils.isEmpty(playMVBean.getUrl())?"": playMVBean.getUrl()));

        //创建MediaController对象
        MediaController mediaController = new MediaController(_mActivity);

        //VideoView与MediaController建立关联
        binding.ttvMv.setMediaController(mediaController);

        //让VideoView获取焦点
        binding.ttvMv.requestFocus();
        binding.ttvMv.start();
        binding.seekbar.setMax(binding.ttvMv.getDuration());


    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        binding.ivBack.setOnClickListener(this);
        binding.ivPlay.setOnClickListener(this);
        binding.seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                MediaPlayerUtils.getInstance().seekTo(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                binding.ttvMv.seekTo(progress);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                pop();
                break;
            case R.id.iv_play:
                if (MVMediaPlayerUtils.getInstance().isPlaying()) {
                    MVMediaPlayerUtils.getInstance().pause();
                    binding.ivPlay.setImageResource(R.mipmap.bofangye_bofang);

                } else {
                    if (MVMediaPlayerUtils.getInstance().isRunning()==false){
                        MVMediaPlayerUtils.getInstance().start();
                    }else {
                        MVMediaPlayerUtils.getInstance().resume();
                    }

                    binding.ivPlay.setImageResource(R.mipmap.bofangye_zanting);
                }
                break;
        }
    }

    @Override
    public void onDestroy() {
        MVMediaPlayerUtils.getInstance().destory();
        super.onDestroy();
    }
}